package cn.yikaojiuguo.student.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.support.utils.MyImgLoader;
import cn.yikaojiuguo.student.support.utils.ViewUtils;

/**
 * Created by cfan_yang on 2016/1/15.
 */
public class AskAdapter extends SimpleBaseAdapter<String>{

    public static final int MAX_NUM = 3;

    public AskAdapter(Context context, List<String> list) {
        super(context, list);
    }

    @Override
    public int getCount() {
        if(mList.size() < MAX_NUM) {
            return mList.size() + 1;
        } else {
            return mList.size();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_ask,null);
            holder = new ViewHolder();
            holder.iv = ViewUtils.find(convertView,R.id.iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(position == mList.size()) {
            if(position == MAX_NUM) {
                holder.iv.setVisibility(View.GONE);
            } else {
                holder.iv.setTag("add");
                holder.iv.setImageResource(R.drawable.append_diploma);
            }
        } else {
            MyImgLoader.getInstance(3, MyImgLoader.Type.FILO).loadImg(getItem(position), holder.iv);
        }
        return convertView;
    }

    @Override
    public void addFirstPageData(List<String> list) {
        if (list == null) {
            return;
        }
        mList.clear();
        MyImgLoader.getInstance(3, MyImgLoader.Type.FILO).clearCache();
        add(list);
    }

    static class ViewHolder {
        public ImageView iv;
    }
}
